// framework for trajctrl

/*
Copyright 2010 Anthony Oko, Koby Hitchen, Mara Cairo, Shaochen Xu, Tyler Lucas

   This file is part of the trajctrl program.

   trajctrl is licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <stdlib.h>
#include <avr/io.h>				// pin definitions
#include <stdint.h>
#include <avr/wdt.h>			// watchdog
#include <avr/interrupt.h>
#include <util/delay.h>

#include "sandbox/adc.h"
#include "sandbox/dac.h"
#include "sandbox/greeting.h"
#include "sandbox/laser.h"
#include "ledmsg.h"
#include "stageinfo.h"
#include "sandbox/system_init.h"
#include "usart.h"

#define F_CPU 16000000UL	// external oscillator frequency
#define PD_CHAN	0
#define PD_AMUX 0x03
#define PD_PRE 16			// 1 MHz ADC
#define PD_VOLT 4/5*1024

// Function Prototypes
void soft_reset();

int main(void)
{	
	
	// boot up properly and set environment variables --> POST
	system_init();
	
	// get run parameters
	greeting_return params = greeting();
	for(uint8_t i=0;i<3;i++) { tx('.'); _delay_ms(50); } // ...
	uint8_t smask[14] = params.smask;
	txstr("Dropping bead...\n");
	
	sol.on(100);		// drop bead
	
	// impulse detector & timer1 initialization
	uint8_t Vimp = init_adc_t1(PD_CHAN,PD_AMUX,PD_PRE,params.qpd[0].tapprox,PD_VOLT);

	for(uint8_t i

			/*
			 Correct timing, speed, and acceleration for all stages.
			 Push ADC values and corrections (or corrected values) to stack.
			 (Maybe we can send them to the PC to free memory? Do we have time?)
			 */

		/*
		 Perform nuclear reaction. Remember to wear sunglasses, and, if a civilean
		 wanders into the blast zone, to not save them, otherwise we'll be turned
		 into Hulks (unless this is your goal).
		 */
//		nuke(* pTIMER_OFFSET, * pTRAJ_OFFSET);

		/* 
		 Send all values to PC.
		 */
}

// ********************* END OF MAIN ********************* //

/*
void setup_adc(uint8_t qpd_stage)
{
  	DDRA &= 0x00;  //Set port A as input
   	PORTA &= 0x00;  //Set port A as tri-state (high impedance)
	ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz 
	ADMUX |= (0<<REFS1)|(0<<REFS0); // Set ADC reference to AREF
   	ADMUX |= (1<<ADLAR); // Left ajdust result to allow easy 8 bit reading 
   	ADMUX &= ~(0x1F);  //Clear bits 0-4 thereby selecting channel 0 of ADC
  	ADCSRA |= (1 << ADATE); //Enable auto-triggering
   	SFIOR |= (0<<ADTS2)|(0<<ADTS1)|(0<<ADTS0); // Set ADC to Free-Running Mode 
   	ADCSRA |= (1 << ADEN);  // Enable ADC 
	ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt 
   	//sei();   // Enable Global Interrupts 
}
   	 
uint8_t* adc_data(uint8_t qpd_stage)
{ 	
	uint8_t *adc_result=new uint8_t[8];
	
	ADCSRA |= (1 << ADSC);  // Start A2D Conversions
	for(uint8_t chan=1;chan<=8;chan++)
   	{ 
   		while(!(ADCSRA & (1<<ADIF))){} //Check if ADIF has set (conversion complete)
		ADCSRA &= ~(1<<ADIF); //Clear ADIF so new conversion can begin
		adc_result[chan-1]= ADCH;
		*adc_result[chan-1]= ADCH;
	} 
	ADCSRA &= ~(1 << ADEN);  // Disenable ADC
	return adc_result;
} 

int8_t lookup_table(uint8_t* qpd_values)
{
	cellA = *(qpd_values);  //top left cell of qpd
	cellB = *(qpd_values+1);  //top right cell of qpd
	cellC = *(qpd_values+2);  //bottom left cell of qpd
	cellD = *(qpd_values+3);  //bottom right cell of qpd
	
	voltage = ????//do calculations here from table we still need to make
	return voltage;
}

void dac_output(uint8_t voltage)
{
	DDRB |= 0xFF; //set port B as output port
	PORTB = voltage; //assign voltage b/w 4-4.2V to base of bjt
}

void laser()
{
	DDRB |= 0xFF; //set port B ???? as output port
	PORTB |= (1<<PB7) //turn on laser
}
*/
/*
 Interrupt Service Routines
 I've put these in the main program because they won't show up anywhere in the main
 program otherwise, even symbolically. Don't want to forget they're here!
 (See Table 18, Reset and Interrupt Vectors, on pg. 44 of the Atmega32 datasheet.)
 (See pp. 258-273 of the avr-libc manual, version 1.6.8, but any recent version should do.)
 n	addr.	source				libc-avr			details
 -	-----	------				--------			-------
 1	$000	RESET				RESET				External Pin, Power-on, Brown-out, Watchdog, and JTAG AVR
 2	$002	INT0				INT0_vect			External Interrupt Request 0 
 3	$004	INT1				INT1_vect			External Interrupt Request 1 
 4	$006	INT2				INT2_vect			External Interrupt Request 2 
 5	$008	TIMER2 COMP			TIMER2_COMP_vect	Timer/Counter2 Compare Match 
 6	$00A	TIMER2 OVF			TIMER2_OVF_vect		Timer/Counter2 Overflow 
 7	$00C	TIMER1 CAPT			TIMER1_COMP_vect	Timer/Counter1 Capture Event 
 8	$00E	TIMER1 COMPA		TIMER1_COMPA_vect	Timer/Counter1 Compare Match A 
 9	$010	TIMER1 COMPB		TIMER1_COMPB_vect	Timer/Counter1 Compare Match B 
 10	$012	TIMER1 OVF			TIMER1_OVF_vect		Timer/Counter1 Overflow
 11	$014	TIMER0 COMP			TIMER0_COMP_vect	Timer/Counter0 Compare Match 
 12	$016	TIMER0 OVF			TIMER0_OVF_vect		Timer/Counter0 Overflow 
 13	$018	SPI, STC			SPI_STC_vect		Serial Transfer Complete 
 14	$01A	USART, RXC			USART_RXC_vect		USART, Rx Complete 
 15	$01C	USART, UDRE			USART_UDRE_vect		USART Data Register Empty 
 16	$01E	USART, TXC			USART_TXC_vect		USART, Tx Complete 
 17	$020	ADC					ADC_vect			ADC Conversion Complete 
 18	$022	EE_RDY				EE_RDY_vect			EEPROM Ready 
 19	$024	ANA_COMP			ANA_COMP_vect		Analog Comparator 
 20	$026	TWI					TWI_vect			Two-wire Serial Interface 
 21	$028	SPM_RDY				SPM_RDY_vect		Store Program Memory Ready
 */

ISR(BADISR_vect)
{
    for(uint8_t i=0;i<10;i++)
		ledmsg("-.");
	soft_reset();
}

/*ISR(RESET)
{
	soft_reset();
}*/

void soft_reset()
{
	wdt_enable(WDTO_15MS);
	while(1) {};
}
